<?php
namespace Admin\Controller;
use Think\Controller;
class VoteController extends CommonController{


	/**
	 * 添加投票专题
	 */
	public function add_vote_special(){

		if(IS_GET){

			$this->display();
		}else{

			$name = I('post.name');
			if(!empty($name)){

				$data['name'] = $name;
			}

			$head_banner_id = I('post.head_banner_id');
			// echo $head_banner_id; die;
			if(!empty($head_banner_id)){

				$data['head_banner'] = $head_banner_id;
			}

			$foot_banner_id = I('post.foot_banner_id');
			if(!empty($foot_banner_id)){

				$data['foot_banner'] = $foot_banner_id;
			}

			$btime = I('post.btime');
			if(!empty($btime)){

				$data['btime'] = strtotime($btime);
			}

			$etime = I('post.etime');
			if(!empty($etime)){

				$data['etime'] = strtotime($etime);
			}

			$data['ctime'] = I('server.REQUEST_TIME');

			// print_r($data); die;
			$vote_specials = M('vote_specials');

			$result = $vote_specials->data($data)->add();

			if(!empty($result)){

				$this->success('添加投票专题成功!',U('Admin/Vote/vote_special_lists'),1);
			}else{

				$this->error('添加投票专题失败!','',1);
			}

		}
	}


	/**
	 * 投票专题列表
	 */
	public function vote_special_lists(){

		if(IS_GET){

			$vote_specials = M('vote_specials');

			/**
			数据分页显示开始
			**/
			$count      = $vote_specials->where(array('ishidden' => 0))->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $vote_specials->where(array('ishidden' => 0))->limit($Page->firstRow.','.$Page->listRows)->select();
			/**
			数据分页结束
			**/
			/**
			 * 计算参赛作品数量
			 */
			$vote_options = M('vote_options');
			foreach ($list as $key => $value) {
				# code...
				$num = $vote_options->where(array('vsid' => $value['id']))->count();
				$list[$key]['option_count'] = $num;

				$list[$key]['link_real'] ='http://'.I('server.HTTP_HOST').U('M/Vote/index',array('vsid' => $value['id']));
			}
			// print_r($list); die;
			$this->assign('show',$show);
			$this->assign('list',$list);
			$this->display();
		}
	}


	/**
	 * 修改投票专题
	 */
	public function modify_vote_special(){

		if(IS_GET){

			$id = I('get.id');
			// echo $id; die;
			if(!empty($id)){

				$map['id'] = $id;
			}else{

				$this->error('参数出错!','',1);
			}

			$vote_specials = M('vote_specials');
			$fundings_image = M('fundings_image');

			$list = $vote_specials->where($map)->find();
			$list['head_banner'] = $fundings_image->where(array('id' => $list['head_banner']))->getField('img_url');
			$list['foot_banner'] = $fundings_image->where(array('id' => $list['foot_banner']))->getField('img_url');

			$this->assign('list',$list);
			// print_r($list); die;
			$this->display();
		}else{

			$id = I('post.id');
			if(!empty($id)){

				$map['id'] = $id;
			}else{

				$this->error('参数出错!','',1);
			}

			$name = I('post.name');
			if(!empty($name)){

				$data['name'] = $name;
			}

			$head_banner_id = I('post.head_banner_id');
			// echo $head_banner_id; die;
			if(!empty($head_banner_id)){

				$data['head_banner'] = $head_banner_id;
			}

			$foot_banner_id = I('post.foot_banner_id');
			if(!empty($foot_banner_id)){

				$data['foot_banner'] = $foot_banner_id;
			}

			$btime = I('post.btime');
			if(!empty($btime)){

				$data['btime'] = strtotime($btime);
			}

			$etime = I('post.etime');
			if(!empty($etime)){

				$data['etime'] = strtotime($etime);
			}

			// $data['ctime'] = I('server.REQUEST_TIME');

			// print_r($data); die;
			$vote_specials = M('vote_specials');

			$result = $vote_specials->where($map)->data($data)->save();

			if(!empty($result)){

				$this->success('修改投票专题成功!',U('Admin/Vote/vote_special_lists'),1);
			}else{

				$this->error('修改投票专题失败!','',1);
			}
		}
	}


	/**
	 * 删除投票专题
	 */
	
	public function del_vote_special(){

		if(IS_AJAX){

			$id = I('post.id');
			// echo $id; die;
			if(!empty($id)){

				$map['id'] = $id;
				$data['ishidden'] = 1;
			}

			$vote_specials = M('vote_specials');

			$result = $vote_specials->where($map)->data($data)->save();
			// var_dump($result); die;
			if($result){

				echo json_encode(1);
			}else{

				echo json_encode(0);
			}
		}
	}


	/**
	 * 修改参赛作品
	 */
	public function modify_vote_option(){

		if(IS_GET){

			$id = I('get.id');
			$vsid = I('get.vsid');
			$isconfirm = I('get.isconfirm');
			// echo $vsid; die;
			$vote_options = M('vote_options');

			$sql = "select fd_vote_options.*,fd_members.username as username from fd_vote_options left join fd_members on fd_vote_options.mid=fd_members.id where fd_vote_options.id=".$id;

			$list = $vote_options->query($sql);
			// echo $vote_options->getLastSql();
			$list[0]['imgs'] = $this->get_image_by_id($list[0]['img_ids']);

			// print_r($list); die;
			$this->assign('isconfirm',$isconfirm);
			$this->assign('vsid',$vsid);
			$this->assign('list',$list);
			$this->display();
		}else{

			$id = I('post.id');
			$vsid = I('post.vsid');
			$isconfirm = I('post.isconfirm');
			if(!empty($id)){

				$map['id'] = $id;
			}

			$content = I('post.content');
			if(!empty($content)){

				$data['content'] = $content;
			}

			// $comments = I('post.comments');
			// if(!empty($comments)){

			// 	$data['comments'] = $comments;
			// }

			$vote_options = M('vote_options');

			$result = $vote_options->where($map)->data($data)->save();

			if($result){
				// echo $vsid; die;
				$back_url = U('Admin/Vote/vote_option_lists',array('vsid' => $vsid,'isconfirm' =>$isconfirm));
				// echo $back_url; die;
				$this->success('修改成功!',$back_url,1);
			}else{

				$this->error('修改失败!','',1);
			}
		}
	}


	/**
	 * 投票作品表
	 */
	public function vote_option_lists(){

		if(IS_GET){

			$vsid_s = I('get.vsid');
			$isconfirm = I('get.isconfirm');

			if(empty($isconfirm))
			{
				$isconfirm=0;
			}


			if(isset($vsid_s)){

				$vsid = $vsid_s;
			}
			// echo $vsid; die;
			$vote_options = M('vote_options');

			/**
			数据分页显示开始
			**/
			$count      = $vote_options->where(array('vsid' => $vsid,'isconfirm' => $isconfirm))->count();// 查询满足要求的总记录数
			// echo $count; die;
			$Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$sql = "select fd_vote_options.*,fd_members.username as username from fd_vote_options left join fd_members on fd_vote_options.mid=fd_members.id where vsid=".$vsid." and isconfirm=".$isconfirm." order by id desc limit ".$Page->firstRow.",".$Page->listRows;

			$list = $vote_options->query($sql);
			// echo $vote_options->getLastSql();
			/**
			数据分页结束
			**/
			// print_r($list); die;
			/**
			 * 获取用户上传的图片
			 */
			foreach ($list as $key => $value) {
				# code...
				$list[$key]['imgs'] = $this->get_image_by_id($value['img_ids']);
			}

			// print_r($list); die;
			$this->assign('show',$show);
			$this->assign('vsid',$vsid);
			$this->assign('list',$list);
			$this->display();
		}else{
			$vote_options = M('vote_options');

			$vsid_s = I('post.vsid');
			$cash_code_s = I('post.cash_code');
			if(!empty($cash_code_s)){

				$map['cash_code'] = $cash_code_s;
				$vsid = $vsid_s;

				$list = $vote_options->where($map)->select();
			}

			// print_r($list);die;

			$this->assign('show',$show);
			$this->assign('vsid',$vsid);
			$this->assign('list',$list);
			$this->display();
		}
	}


	/**
	 * 删除参赛作品
	 */
	public function del_vote_option(){

		if(IS_AJAX){

			$id = I('post.id');
			$ishidden = I('post.ishidden');
			if(!empty($id)){

				$map['id'] = $id;
				if($ishidden == 0){

					$data['ishidden'] = 1;
				}else{

					$data['ishidden'] = 0;
				}
			}

			$vote_options = M('vote_options');
			$result = $vote_options->where($map)->data($data)->save();

			if($result){

				echo json_encode(1);
			}else{

				echo json_encode(0);
			}
		}
	}

	/**
	 * 投票列表
	 */
	public function vote_record_lists(){

		if(IS_GET){

			$id = I('get.id');
			// echo $vsid; die;
			$vote_records = M('vote_records');

			/**
			数据分页显示开始
			**/
			$count      = $vote_records->where(array('vid' => $id))->count();// 查询满足要求的总记录数
			// echo $count; die;
			$Page       = new \Think\Page($count,40);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>40</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$sql = "select fd_vote_records.*,fd_members.username as username from fd_vote_records left join fd_members on fd_vote_records.mid=fd_members.id where vid=".$id." order by id desc limit ".$Page->firstRow.",".$Page->listRows;

			$list = $vote_records->query($sql);


			// print_r($list); die;
			$this->assign('show',$show);
			$this->assign('list',$list);
			$this->display();
		}
	}

	/**
	 * 删除投票记录
	 */
	public function del_vote_record(){

		$id = I('post.id');
		$vid = I('post.vid');
		// echo $id; echo $vid; die;
		if(empty($id)){

			echo json_encode(0);
		}
		if(empty($vid)){

			echo json_encode(0);
		}

		$vote_records = M('vote_records');
		$vote_options = M('vote_options');

		$ares = $vote_records->where(array('id' => $id))->delete();

		if($ares){

			$map['id'] = $vid;

			$bres = $vote_options->where($map)->setDec('vote_count');

			if($bres){

				echo json_encode(1);
			}else{

				echo json_encode(0);
			}

		}else{

			echo json_encode(0);
		}
	}



	/**
	 * ajax上传图片
	 */
	public function upload_image(){

		if(IS_AJAX){

			$file = I('post.file');
			$up_location = 'fundings/Vote/';
			$t_url = __ROOT__.'/Uploads/'.$this->upload($_FILES[$file],$up_location);

			$data['img_url'] = $t_url;

			$fundings_image = M('fundings_image');

			$t_id = $fundings_image->data($data)->add();

			$result['tid'] = $t_id;
			$result['turl'] = $t_url;

			if(!empty($result)){

				echo json_encode($result);
			}else{

				echo json_encode(0);
			}
		}
	}


	//  文件上传 图片格式上传
	private function upload($file,$up_location){

		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     31457280 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =      $up_location; // 设置附件上传目录
		// 上传文件 
		$info   =   $upload->uploadOne($file);
		if(!$info){
		// 上传错误提示错误信息    
			$this->error($upload->getError());
		}else{   
			$file_url=$info['savepath'].$info['savename'];    
			return $file_url;
		}
	}


	/**
	 * 通过id获取图片的地址
	 */
	private function get_image_by_id($img_ids){

		$ids = explode(',', $img_ids);

		$fundings_image = M('fundings_image');

		foreach ($ids as $key => $value) {
			
			$img[] = $fundings_image->where(array('id' => $value))->getField('img_url');
		}

		return $img;
	}




	/**
	 * 彻底删除参赛作品
	 */
	
	public function delete_vote_option(){


		if(IS_AJAX){

			$id = I('post.id');
			if(!empty($id)){
				$map['id'] = $id;
			}

			$vote_options = M('vote_options');

			$result = $vote_options->where($map)->delete();
			//当删除参赛祖品成功后 继续删除该祖品下的投票记录
			if($result){

				$res = $this->delete_vote_records($id);
			}

			if($result){

				echo json_encode(1);
			}else{

				echo json_encode(0);
			}
		}
	}



	/**
	 * 根据参赛作品id删除下面的所有投票记录
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	private function delete_vote_records($id){

		$vid = $id;
		if(!empty($vid)){

			$map['vid'] = $vid;
		}else{

			return false;
		}

		$vote_records = M('vote_records');

		$res = $vote_records->where($map)->delete();

		return $res;
	}




	/**
	 * 审核通过参赛作品
	 */
	public function agree_pass(){

		if(IS_AJAX){

			$id = I('post.id');
			if(!empty($id)){
				$map['id'] = $id;
				$data['isconfirm'] = 1;
			}

			$vote_options = M('vote_options');

			$result = $vote_options->where($map)->data($data)->save();


			if($result){

				echo json_encode(1);
			}else{

				echo json_encode(0);
			}
		}
	}


	/**
	 * ajax 修改option备注
	 */
	public function ajax_modify_option_comments(){

		if(IS_AJAX){

			$vid = I('post.vid');
			if(!empty($vid)){

				$map['id'] = $vid;
			}

			$comments = I('post.comments');
			if(isset($comments)){

				$data['comments'] = $comments;
			}

			$vote_options = M('vote_options');

			$res = $vote_options->where($map)->data($data)->save();

			if($res){

				$data = $comments;
				echo json_encode($data);
			}else{
				$data = $comments;
				echo json_encode($data);
			}
		}
	}




	public function count_all_money(){

		$DB_vote_options = M('vote_options');

		$list = $DB_vote_options->getField('comments',true);
		// print_r($list); die;
		foreach ($list as $key => $value) {
			# code...
			if(empty($value)){

				unset($list[$key]);
			}else{

				$list[$key] = explode('，',$value);
			}
		}
		// print_r($list);die;
		foreach ($list as $key => $value) {
			# code...
			$counts[$key] = count($value);
		}
		// print_r($counts); die;

		foreach ($list as $key => $value) {
			# code...
			$num = $counts[$key] - 1;
			$res[$key] = $value[$num];
		}

		// print_r($res); die;

		foreach ($res as $key => $value) {
			# code...
			if($value >=5){

				$res[$key] = ($value-5)*5+11;
			}else{

				switch ($value) {
					case 1:
						$res[$key] = 1;
						break;
					case 2:
						$res[$key] = 2;
						break;
					case 3:
						$res[$key] = 4;
						break;
					
					default:
						$res[$key] = 7;
						break;
				}
			}
		}

		// print_r($res); die;	


		$sum_money = array_sum($res);
		echo $sum_money;

	}
}